package com.kbbywyclient.ui.adapter;
import android.widget.ImageView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.HotRec;
import com.kbbywyclient.ui.adapter.base.BaseAdapter;
import com.kbbywyclient.util.ImageLoaderUtils;
import com.kbbywyclient.util.StringUtil;

import java.util.List;

/**
 * Created by Jiangzx on 2017/8/16 9:59.
 */

public class HotRecAdapter extends BaseAdapter<HotRec> {

    public interface OnItemClickedListener{
        int ACTION_GO_DETAILED =1;
        int ACTION_SHARE=2;
        void onItemClicked(HotRec hotRec,int action);
    }

    private OnItemClickedListener listener;

    public void setOnItemClickedListener(OnItemClickedListener listener){
        this.listener=listener;
    }

    private int radius;

    public HotRecAdapter(List<HotRec> data) {
        super(data, R.layout.item_index_hot_rec);
        radius= StringUtil.dip2px(5);
    }

    @Override
    public void convert(Holder holder, final HotRec bean, int position) {
        holder.setText(R.id.tv_title,bean.getName());
        ImageView imageView=holder.getView(R.id.iv_pic);
        ImageLoaderUtils.displayImage(imageView,
                API.PIC_ADDRESS+bean.getImage(),
                radius);
        List<HotRec.PrivilegesListBean> labels=bean.getPrivilegesList();
        holder.setText(R.id.tv_label,handlerLabels(labels));

        holder.setText(R.id.tv_share_earn,"分享赚￥"+bean.getMoney());
        holder.getView(R.id.tv_share_earn).setOnClickListener(view->{
            if(listener!=null){
                listener.onItemClicked(bean,OnItemClickedListener.ACTION_SHARE);
            }
        });
        holder.itemView.setOnClickListener(view->{
            if(listener!=null){
                listener.onItemClicked(bean,OnItemClickedListener.ACTION_GO_DETAILED);
            }
        });
    }

    public static String handlerLabels(List<HotRec.PrivilegesListBean> labels){
        StringBuilder sb=new StringBuilder("");
        if(labels!=null&&labels.size()>0){
            List<HotRec.PrivilegesListBean> temp;
            if(labels.size()>3){
                temp=labels.subList(0,3);
            }else{
                temp=labels;
            }
            for(int i=0,size=temp.size();i<size;i++){
                sb.append(temp.get(i).getName());
                if(i!=size-1){
                    sb.append("\n");
                }
            }
        }
        return sb.toString();
    }
}
